package demoMod.nanaki.ui.orb;

import basemod.abstracts.CustomEnergyOrb;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import demoMod.nanaki.NanakiMod;

public class NanakiEnergyOrb extends CustomEnergyOrb {

    private static final float ORB_IMG_SCALE = 1.15F * Settings.scale;

    protected Texture baseLayer;

    protected Texture[] energyLayers;

    protected Texture[] noEnergyLayers;

    protected Texture orbVfx;

    protected float[] layerSpeeds;

    protected float[] angles;

    private float sleepTimer;

    public NanakiEnergyOrb() {
        super(null, null, null);
        this.energyLayers = new Texture[2];
        this.noEnergyLayers = new Texture[2];
        this.baseLayer = ImageMaster.loadImage(NanakiMod.getResourcePath("UI/EPanel/layer0.png"));
        this.energyLayers[0] = ImageMaster.loadImage(NanakiMod.getResourcePath("UI/EPanel/layer1.png"));
        this.noEnergyLayers[0] = ImageMaster.loadImage(NanakiMod.getResourcePath("UI/EPanel/layer1.png"));
        this.energyLayers[1] = ImageMaster.loadImage(NanakiMod.getResourcePath("UI/EPanel/layer2.png"));
        this.noEnergyLayers[1] = ImageMaster.loadImage(NanakiMod.getResourcePath("UI/EPanel/layer2.png"));
        this.orbVfx = ImageMaster.loadImage(NanakiMod.getResourcePath("UI/EPanel/vfx.png"));
        this.layerSpeeds = new float[]{-5.0F};
        this.angles = new float[this.layerSpeeds.length];
        this.sleepTimer = 0.0F;
        assert this.energyLayers.length == this.layerSpeeds.length;
    }

    @Override
    public Texture getEnergyImage() {
        return this.orbVfx;
    }

    @Override
    public void updateOrb(int energyCount) {

        if (energyCount == 0) {
            this.sleepTimer += Gdx.graphics.getDeltaTime();
            if (this.sleepTimer > 1.0F) this.sleepTimer = 1.0F;
        } else {
            this.sleepTimer -= Gdx.graphics.getDeltaTime();
            if (this.sleepTimer < 0.0F) this.sleepTimer = 0.0F;
        }
    }

    @Override
    public void renderOrb(SpriteBatch sb, boolean enabled, float current_x, float current_y) {
        sb.setColor(Color.WHITE);
        sb.draw(this.baseLayer, current_x - 64.0F - 25.0F, current_y - 64.0F - 10.0F, 64.0F + 20.0F, 64.0F + 10.0F, 168.0F, 148.0F, ORB_IMG_SCALE, ORB_IMG_SCALE, 0, 0, 0, 168, 148, false, false);
        float tx = current_x;
        float ty = current_y;
        float d = (InputHelper.mX - current_x) * (InputHelper.mX - current_x) + (InputHelper.mY - current_y) * (InputHelper.mY - current_y);
        if (d < 40.0F) {
            tx = InputHelper.mX;
            ty = InputHelper.mY;
        } else {
            float ts = (float) Math.sqrt(40.0F / d);
            tx = tx + (InputHelper.mX - tx) * ts;
            ty = ty + (InputHelper.mY - ty) * ts;
        }
        sb.draw(this.energyLayers[0], tx - 64.0F - 25.0F, ty - 64.0F - 10.0F, 64.0F + 20.0F, 64.0F + 10.0F, 168.0F, 148.0F, ORB_IMG_SCALE, ORB_IMG_SCALE, 0, 0, 0, 168, 148, false, false);
        sb.draw(this.energyLayers[1], current_x - 64.0F - 25.0F, current_y - 64.0F - 10.0F, 64.0F + 20.0F, 64.0F + 10.0F, 168.0F, 148.0F, ORB_IMG_SCALE, ORB_IMG_SCALE, 0, 0, 0, 168, 148, false, false);
    }
}
